package com.chixing.day14.day14_Ho;

import com.chixing.day13.day13_Home.ProductS;

import java.util.*;
import java.util.function.Consumer;

public class ProductTest {
    public static void main(String[] args){
        List<Product> list = new ArrayList<>();
        Product p1 = new Product("宝洁","洗手粉",18.5);
        Product p2 = new Product("联合利华","肥皂",4.5);
        Product p3 = new Product("宝洁","牙膏",32.5);
        Product p4 = new Product("宝洁","毛巾",14.5);
        Product p5 = new Product("洁利","洗面奶",26.0);
        Product p6 = new Product("好迪","洗发水",27.5);
        Product p7 = new Product("多芬","沐浴露",38.5);
        Product p8 = new Product("宝洁","洗洁精",3.4);
        list.add(p1);
        list.add(p2);
        list.add(p3);
        list.add(p4);
        list.add(p5);
        list.add(p6);
        list.add(p7);
        list.add(p8);


        Map<String,Double> map = new HashMap<>();
        for (Product p : list) {
            if(map.containsKey(p.getBrand())){
                map.put(p.getBrand(),map.get(p.getBrand()) + p.getCost());
            }else{
                map.put(p.getBrand(),p.getCost());
            }

        }
        /*List<Map.Entry<String,Double>> newList = new ArrayList<>();
        Set<Map.Entry<String,Double>> entries = map.entrySet();
        Iterator<Map.Entry<String,Double>> it = entries.iterator();
        while (it.hasNext()) {
            newList.add(it.next());
        }

        newList.sort(new Comparator<Map.Entry<String,Double>>() {
            @Override
            public int compare(Map.Entry<String, Double> o1, Map.Entry<String, Double> o2) {
                return (int) (o2.getValue() - o1.getValue());

            }
        });
        newList.forEach(new Consumer<Map.Entry<String, Double>>() {
            @Override
            public void accept(Map.Entry<String, Double> stringDoubleEntry) {
                System.out.println(stringDoubleEntry);
            }
        });*/


        stringDouble(map);
        }


    private static void stringDouble(Map<String, Double> map) {
        Set<String> str = map.keySet();
        int len = str.size();
        for (int i = 0; i < len; i++) {
            double max = 0;
            String str1 = null;
            for (String str2 : str) {
                if(max < map.get(str2)){
                    max = map.get(str2);
                    str1 = str2;
                }
            }
            System.out.println(str1 + " - " + max);
            map.remove(str1);
        }
    }
}

